package com.ymy.thinking.in.spring.bean.lifecycle;

import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Bean 元信息配置示例
 *
 * {@link BeanDefinitionReader} 实际上是面向资源读取 Bean Definition
 *
 * @author Ringo
 * @date 2022/2/5 22:47
 */
public class BeanConfigurationMetadataReaderDemo {

    public static void main(String[] args) {
        final String location = "classpath:META-INFO/employee.properties";
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(BeanConfigurationMetadataReaderDemo.class);

        // BeanDefinitionReader 读取 properties 中的配置元信息
        BeanDefinitionReader beanDefinitionReader = new PropertiesBeanDefinitionReader(applicationContext);
        int count = beanDefinitionReader.loadBeanDefinitions(location);
        System.out.println("Bean Definition Count: " + count);

        // 启动 Spring 容器
        applicationContext.refresh();

        // 依赖查找
        applicationContext.getBeanProvider(Employee.class).stream().forEach(System.out::println);

        applicationContext.close();
    }

}
